
const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require("html-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const { compilation } = require('webpack')

class MyPlugin{
  apply(compiler){
    console.log("启动")
    compiler.hooks.emit.tap('MyPlugin',compilation => {
      // compilation =>此次打包的上下文
      for(const name in compilation.assets){
        if(name.endsWith('.js')){
          const contents = compilation.assets[name].source()
          const withoutComments = contents.replace(/\/\*\*+\*\//g,'')
          compilation.assets[name] = {
            source: () => withoutComments,
            size : () => withoutComments.length
          }
        }
      }
    })
  }
}
module.exports = {
  mode:"development",
  entry:'./src/index.js',
  output:{
    filename:"main.js",
    path:path.join(__dirname,'dist'),
    // publicPath:'dist/'
  },
  module:{
    rules:[
      {
        test:/.md$/,
        use:['html-loader','./markdown-loader.js']
      }
    ]
  },
  plugins:[
    new CleanWebpackPlugin(),
    //用于生成index.html文件的
    new HtmlWebpackPlugin({
      title:'webpack plugin',
      meta:{
        viewport:'width=device-width'
      },
      template:'./src/demo.html'
    }),
    //用于生成about.html文件
    new HtmlWebpackPlugin({
      filename:'about.html',
      title:'webpack about',
      template:'./src/aboutDemo.html'
    }),
    new CopyWebpackPlugin({
      patterns:[
        { from: 'public', to: './' },
      ]
    }),
    new MyPlugin()
  ]
}